package com.gogant.spider.demo.novel;

import com.gogant.spider.core.SpiderException;
import com.gogant.spider.http.HttpResponse;
import com.gogant.spider.http.HttpSpider;
import com.gogant.spider.http.HttpURLHandler;
import java.util.ArrayList;
import java.util.List;

public class DingDianNovel {

    private HttpSpider spider = new HttpSpider(1);

    public List<SearchInfo> search(String keyword) throws SpiderException {
        final List<SearchInfo> result = new ArrayList<SearchInfo>();
        spider.syncFetch("http://so.23wx.com/cse/search?s=15772447660171623812&entry=1&q=" + keyword, new HttpURLHandler() {
            @Override
            public void onResponse(HttpResponse resp) {
                try {
                    List<SearchInfo> infoList = resp.getBeanList(SearchInfo.class);
                    if (infoList != null) {
                        result.addAll(infoList);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return result;
    }

    public static void main(String[] args) throws SpiderException{
        DingDianNovel ddn = new DingDianNovel();
        for (SearchInfo info : ddn.search("杀神")) {
            System.out.println(info);
        }
    }
}
